package com.health.controller.orderSetting;

import com.alibaba.dubbo.config.annotation.Reference;
import com.health.constant.MessageConstant;
import com.health.entity.PageResult;
import com.health.entity.QueryPageBean;
import com.health.entity.Result;
import com.health.pojo.OrderList;
import com.health.pojo.OrderSetting;
import com.health.pojo.User;
import com.health.service.OrderService;
import com.health.service.OrderSettingService;
import com.health.service.UserService;
import com.health.utils.POIUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Classname OrderSetting
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/3/16 18:09
 * @Author RainGrd
 */
@RestController
@Slf4j
@RequestMapping("/orderSetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;
    @Reference
    private OrderService orderService;


    @Reference
    private UserService userService;


    @RequestMapping("/upload.do")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) throws IOException {
        int i = 0;
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            //判断读取出来的文件是否为空
            if (list.size() > 0) {
                List<OrderSetting> orderSettingList = new ArrayList<>();
                for (String[] strings : list) {
                    OrderSetting orderSetting =
                            new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1]));
                    orderSettingList.add(orderSetting);
                }
                i = orderSettingService.saveOrderSettingList(orderSettingList);
            }
        } catch (IOException e) {
            log.error(e.getMessage());
            return Result.error();
        }
        return i > 0 ? Result.success(MessageConstant.IMPORT_ORDERSETTING_SUCCESS) : Result.success(MessageConstant.IMPORT_ORDERSETTING_FAIL);
    }


    /**
     * 根据日期查询预约设置数据(获取指定日期所在月份的预约设置数据)
     *
     * @param date
     * @return
     */
    @RequestMapping("/getOrderSettingByMonth.do")
    public Result getOrderSettingByMonth(String date) {//参数格式为：2019-03
        log.info("date:{}", date);
        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            log.info("查询出来的数据:{}", list);
            //获取预约设置数据成功
            return list.size() > 0 ? Result.success(list, MessageConstant.GET_ORDERSETTING_SUCCESS) : Result.error(MessageConstant.GET_ORDERSETTING_FAIL);
        } catch (Exception e) {
            log.error(e.getMessage());
            //获取预约设置数据失败
            return Result.error();
        }
    }

    @RequestMapping("/editNumberByDate.do")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        log.info("要修改的orderSetting:{}", orderSetting);
        return orderSettingService.editOrderSetting(orderSetting);
    }


    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean,@Param("userName") String userName) {
        User user = userService.queryByUserName(userName);
        log.info("queryPageBean:{}", queryPageBean);
        return orderSettingService.queryOrderListPage(queryPageBean, user);
    }

    @RequestMapping("/add.do")
    public Result add(@RequestBody OrderList orderList, Integer checkSetmealId) {
        log.info("orderList:{}", orderList);
        return orderSettingService.saveOrderSettings(orderList, checkSetmealId);
    }


    //根据id查询检查组
    @RequestMapping("/arrived.do")
    public Result arrived(@RequestParam("id") Integer id) {
        try {
            int i = orderSettingService.arrived(id);
            return i > 0 ? Result.success() : Result.error();
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error();//失败
        }
    }

    @RequestMapping("/delete.do")//使其那个被访问到
    public Result delete(@RequestBody OrderList orderList) {
        log.info("orderList:{}", orderList);
        return orderService.deleteOrderSettings(orderList);
    }

    @RequestMapping("/findOrderSettings.do")
    public Result findOrderSettings() {
        return orderSettingService.queryOrderSettings();
    }


}
